package com.labofjet.blogv3.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.labofjet.blogv3.facade.HomeFacade;
import com.labofjet.blogv3.model.Artical;

/**
 * 主页
 * 
 * @author Jet
 * @version 2015.10
 * 
 */
@Controller
public class HomeController {
	@Autowired
	private HomeFacade homeFacade;

	/**
	 * 欢迎界面Controller
	 * 
	 * @param currentPage
	 * @param m
	 * @return
	 */
	@RequestMapping(value = { "/page/{currentPage}" })
	public String index2(@PathVariable int currentPage, Model m) {
		m.addAttribute("currentPage", currentPage);
		return "index2";
	}

	/**
	 * 欢迎界面Controller
	 * 
	 * @param m
	 * @return
	 */
	@RequestMapping(value = { "/index", "/" })
	public String index(Model m) {
		m.addAttribute("currentPage", 1);
		return "index2";
	}

	/**
	 * 类别列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/category-list")
	public String categoryList(Model m) {
		m.addAttribute("categories", homeFacade.findAllCategories());
		return "import/category";
	}

	/**
	 * 顶置文章列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/top-artical-list")
	public String topArticalList(Model m) {
		m.addAttribute("articals", homeFacade.findAllTopArticals());
		return "import/top-articals";
	}

	/**
	 * 首页文章列表
	 * 
	 * @return
	 */
	@RequestMapping("/artical-list/{currentPage}")
	public String articalList(Model m, @PathVariable int currentPage) {
		Page<Artical> temp = homeFacade.findArticals(currentPage - 1);// 数据库从0开始,页面从1开始
		m.addAttribute("articals", temp);
		return "import/articals";
	}

	/**
	 * 历史界面Controller
	 * 
	 * @return
	 */
	@RequestMapping("/history")
	public String history() {
		return "history";
	}

	/**
	 * 历史
	 * 
	 * @return
	 */
	@RequestMapping("/history-list")
	public String historyList(Model m) {
		m.addAttribute("histories", homeFacade.findAllHistory());
		return "import/history";
	}

	/**
	 * 留言界面Controller
	 * 
	 * @return
	 */
	@RequestMapping("/message")
	public String message() {
		return "message";
	}

	/**
	 * 留言
	 * 
	 * @return
	 */
	@RequestMapping("/leave-message")
	public String leaveMessage(Model m) {
		return "import/message";
	}

	/**
	 * 分类界面Controller
	 * 
	 * @return
	 */
	@RequestMapping("/category/{keyword}/{currentPage}")
	public String category(@PathVariable String keyword, @PathVariable int currentPage, Model m) {
		m.addAttribute("keyword", keyword);
		m.addAttribute("currentPage", currentPage - 1);
		return "category";
	}

	/**
	 * 分类
	 * 
	 * @return
	 */
	@RequestMapping("/category-list/{keyword}/{currentPage}")
	public String categoryList(Model m, @PathVariable String keyword, @PathVariable int currentPage) {
		Page<Artical> articals = homeFacade.findCategoryArticals(keyword, currentPage);
		m.addAttribute("articals", articals);
		m.addAttribute("keyword", keyword);
		return "import/category-articals";
	}

	/**
	 * 文章
	 * 
	 * @return
	 */
	@RequestMapping("/artical/{id}")
	public String artical(Model m, @PathVariable long id) {
		Artical a = homeFacade.findArtical(id);
		m.addAttribute("artical", a);
		return "artical";
	}
}
